<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

if (defined('__ROOT__')) {
    define('__ROOT__', dirname(dirname(__FILE__)));
}

/**
 * 
 *
 *
 */
class CPT_Controller extends CI_Controller {

    private     $layout_view    = 'template/main';                              #the template file that is included in every view
    private     $content_view   = '';                                           #the view that is loaded
    protected   $view_data      = array();
    protected   $headline       = 'No headline!';

    public function __construct() {
        parent::__construct();

        //Tank-Auth 

        if (!$this->tank_auth->is_logged_in()) {
            // check for accessed class
            switch ($this->router->class) {  
                // class listing begins here   
                // if we access the auth class, further define the methods                        
                case 'auth':
                    // check for login page requests, else redirect to login
                    switch ($this->router->method) {
                        case 'login':break;
                        case 'check_login' :break;
                        case 'register' :break;
                        default: redirect('auth/login/');
                            break;
                    }
                    break;
                // if class does not match any of the above, redirect to login
                default:
                    redirect('auth/login/');
                    break;
                // class listing ends here
            }
        } 
    }

    protected function set_layout_view($view) {
        $this->layout_view = $view;
    }

    protected function get_layout_view() {
        return $this->layout_view;
    }

    protected function get_content_view() {
        return $this->content_view;
    }

    protected function set_content_view($view) {
        $this->content_view = $view;
    }

    public function _output($output) {
        if ($this->tank_auth->is_logged_in()) {
            $data['username']   = $this->session->userdata('username');
            $data['user_id']    = $this->session->userdata('user_id');
        }
        
        $data['headline']   = $this->headline;
        
        //build the view name depending on controller name and method like (views/class/method)
        //if content_view isn't overwritten by the controller
        if (FALSE !== $this->content_view && empty($this->content_view)) {
            $this->content_view = $this->router->class . '/' . $this->router->method;
        }

        //render the content view
        $data['yield'] = FALSE;
        if (file_exists(APPPATH . 'views/' . $this->content_view . EXT)) {                        #does the view file exist? 
            $data['yield'] = $this->load->view($this->content_view, $this->view_data, TRUE);      #render view and store it in yield
        } else {
            $data['headline']   = 'Error';
            $data['yield']      = 'Content View `' . $this->content_view . '` not found!';
        }
        
        //output the stuff directly if no template is set
        if ($this->layout_view) {
            echo $this->load->view($this->layout_view, $data, TRUE);                           
        } else {
            echo $data['yield'];
        }
    }

    /**
     *
     * 
     */
    public function display_notification($message = '', $type = 'error', $target = FALSE) {
        if('' == $message || FALSE == $target)
            return;
        
        //if session is emtpy, initialize the variable
        if(FALSE == $this->session->flashdata('notification'))
            $old_data = NULL;
        else
            $old_data = $this->session->flashdata('notification');
        
        //decision switch
        switch ($type) {
            case 'success':
                $attr = 'alert alert-success';
                break;
            case 'info':
                $attr = 'alert alert-info';
                break;
            case 'warning':
                $attr = 'alert alert-warning';
                break;
            default:
                $attr = 'alert alert-error';
                break;
        }
        
        $messagedata['type']    = $attr; 
        $messagedata['message'] = $message;
        
        //combine both, old and new notifications
        $new_data['notification'][]   = $old_data;
        $new_data['notification'][]   = $messagedata;
        
        $this->session->set_flashdata($new_data);
        redirect($target);
    }

}

